<template>
  <div>
    <bread :second="'商品管理'" :third="'商品列表'"></bread>
    <!--------------------------------- 卡片 --------------------------------------->
    <el-card class="box-card" style="margin-top:15px ">
      <!--------------------- input 搜索 -------------------------------->
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-input
              placeholder="请输入内容"
              v-model="queryInfo.query"
              class="input-with-select"
              clearable
              @clear="searchGoods()"
            >
              <el-button slot="append" icon="el-icon-search" @click="searchGoods()"></el-button>
            </el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <!--############### 添加按钮 ###############-->
            <el-button type="primary" @click="addGoodsList()">添加商品</el-button>
          </div>
        </el-col>
      </el-row>
      <!------------------------------------ table表格 --------------------------------------------->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column label="#" width="50" type="index"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)" width="120"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量" width="100"></el-table-column>
        <el-table-column prop="add_time" label="创建时间" width="180">
          <template slot-scope="scope">{{scope.row.add_time| dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="editGoods(scope.row)">编辑</el-button>
            <el-button type="danger" size="mini" @click="deleteGoods(scope.row)">删除</el-button>
            <!-- <pre>=={{scope.row}}==</pre> -->
          </template>
        </el-table-column>
      </el-table>
      <!-------------------------------------- 分页区域 ------------------------------------------>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-------------------------------- 编辑按钮弹窗 ---------------------------------->
    <el-dialog title="编辑商品" :visible.sync="editDialogFormVisible">
      <el-form :model="editGoodsForm" :rules="rulesForm" ref="editGoodsFormDialog">
        <el-form-item label="商品名称" :label-width="formLabelWidth" prop="goods_name">
          <el-input v-model="editGoodsForm.goods_name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品价格" :label-width="formLabelWidth" prop="goods_price">
          <el-input v-model="editGoodsForm.goods_price" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品数量" :label-width="formLabelWidth" prop="goods_number">
          <el-input v-model="editGoodsForm.goods_number" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="商品重量" :label-width="formLabelWidth" prop="goods_weight">
          <el-input v-model="editGoodsForm.goods_weight" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editDialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editGoodsBtn()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
           
<script>
import {
  getGoodsList_api,
  delGoodsList_api,
  editGoodsList_api
} from "@/api/index.js";
//在main里面定义全局过滤器，格式化时间-------------------------
export default {
  data() {
    return {
      queryInfo: { query: "", pagenum: 1, pagesize: 10 },
      //表格
      tableData: [],
      //分页
      total: 0,
      //编辑弹窗------
      editDialogFormVisible: false,
      editGoodsForm: {
        goods_id: "",
        goods_name: "",
        good_price: "",
        goods_number: "",
        goods_weight: ""
      },
      formLabelWidth: "120px",
      rulesForm: {
        goods_name: [
          { required: true, message: "请输入商品名称", trigger: "blur" },
          { min: 5, max: 50, message: "长度在 5 到 50 个字符", trigger: "blur" }
        ],
        goods_price: [
          { required: true, message: "请输入商品价格", trigger: "blur" }
        ],
        goods_weight: [
          { required: true, message: "请输入商品重量", trigger: "blur" }
        ],
        goods_number: [
          { required: true, message: "请输入商品数量", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    //获取数据，渲染页面-----------------------
    async getGoodsList() {
      const { data: res } = await getGoodsList_api(this.queryInfo);
      // console.log(res, 11111);
      this.tableData = res.data.goods;
      this.total = res.data.total;
    },
    //搜索商品--------------------
    searchGoods() {},
    //添加商品
    addGoodsList() {
      // 添加编程式路由
      this.$router.push("/goods/add");
    },
    //分页功能------------------------------------
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    //删除商品功能-------------------------------
    async deleteGoods(row) {
      const res = await this.$confirm(
        "此操作将永久删除该文件, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }
      ).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
        return;
      });
      if (res == "confirm") {
        const { data: res } = await delGoodsList_api(row);
        this.$message.success("删除成功");
      }
    },
    //编辑功能----------------------
    editGoods(row) {
      this.editDialogFormVisible = true;
      this.editGoodsForm = row;
      // console.log(this.editGoodsForm, 111111);
    },
    editGoodsBtn() {
      this.$refs.editGoodsFormDialog.validate(async valid => {
        if (valid) {
          const { data: res } = await editGoodsList_api(this.editGoodsForm);
          console.log(res, 333333);
          this.editDialogFormVisible = false;
          //更新页面---重置表单----------------
          this.getGoodsList();
           this.$refs.editGoodsFormDialog.resetFields();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>
<style scoped>
</style>